package cn.dlc.com.biz.room.controller;

import cn.dlc.com.biz.core.Constants;
import cn.dlc.com.biz.room.entity.Room;
import cn.dlc.com.biz.user.entity.User;
import cn.hutool.core.lang.UUID;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.dlc.com.biz.core.Constants.ROOM_USER_PREFIX;

/**
 * @Author dlc
 * @Date 2024/1/8
 * @Description TODO
 * @Version 0.0.1
 */

@RestController
@RequestMapping("room")
@Api(tags = {"房间相关"})
public class RoomController {

    @Autowired
    private StringRedisTemplate redisTemplate;


    @ApiOperation(value = "创建房间", tags = {"房间相关"})
    @PostMapping
    public Map<String, Object> createRoom(@RequestBody Room room){

        Map<String, Object> result = new HashMap<>();

        // 先判断房间存在与否
        List<String> roomListStr = redisTemplate.opsForList().range(Constants.ROOM_LIST_PREFIX, 0, -1);
        if(roomListStr != null && !roomListStr.isEmpty()) {
            List<Room> roomList = roomListStr.stream().map(str -> {
                return JSONObject.parseObject(str, Room.class);
            }).collect(Collectors.toList());

            for (Room serverRoom : roomList) {
                if(serverRoom.getRoomNumber().equals(room.getRoomNumber()) || serverRoom.getRoomName().equals(room.getRoomName())) {
                    // 房间同名，或者房间同号都不行
                    result.put("error", "房间编号或者房间名称重复，不能创建房间");
                    return result;
                }
            }
        }

        room.setId(UUID.fastUUID().toString());
        String roomValue = JSONObject.toJSONString(room);
        // 添加到房间列表里面
        redisTemplate.opsForList().leftPush(Constants.ROOM_LIST_PREFIX, roomValue);

        result.put("roomInfo", room);
        return result;
    }

    @ApiOperation(value = "房间列表", tags = {"房间相关"})
    @GetMapping("list")
    public Map<String, Object> listRoom(){

        Map<String, Object> result = new HashMap<>();

        List<Room> roomList = new ArrayList<>();
        // 添加到房间列表里面
        List<String> roomListStr = redisTemplate.opsForList().range(Constants.ROOM_LIST_PREFIX, 0, -1);

        if(roomListStr != null && !roomListStr.isEmpty()) {
            roomList = roomListStr.stream().map(str -> {
                return JSONObject.parseObject(str, Room.class);
            }).collect(Collectors.toList());
        }
        result.put("roomList", roomList);
        return result;
    }

    @ApiOperation(value = "关闭房间", tags = {"房间相关"})
    @PostMapping("close/{roomNumber}")
    public Map<String, Object> closeRoom(@PathVariable String roomNumber){

        Map<String, Object> result = new HashMap<>();
        List<Room> roomList = new ArrayList<>();
        // 添加到房间列表里面
        List<String> roomListStr = redisTemplate.opsForList().range(Constants.ROOM_LIST_PREFIX, 0, -1);

        if(roomListStr != null && !roomListStr.isEmpty()) {
            for (String roomStr : roomListStr) {
                if(roomStr.contains(roomNumber)) {
                    redisTemplate.opsForList().remove(Constants.ROOM_LIST_PREFIX, 0, roomStr);

                    // 删掉这个房间里面所有的人
                    redisTemplate.delete(ROOM_USER_PREFIX + roomNumber);
                }
            }
            // 删掉之后 重新查一次
            roomListStr = redisTemplate.opsForList().range(Constants.ROOM_LIST_PREFIX, 0, -1);

            if(roomListStr != null && !roomListStr.isEmpty()) {
                roomList = roomListStr.stream().map(str -> {
                    return JSONObject.parseObject(str, Room.class);
                }).collect(Collectors.toList());
            }
        }
        result.put("roomList", roomList);

        return result;
    }



}
